//
//  ChangeSignaureVC.m
//  Cunli
//
//  Created by lilubin on 15/6/30.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ChangeSignaureVC.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Config.h"
#import "User.h"

@interface ChangeSignaureVC ()
@property (weak, nonatomic) IBOutlet UITextView *signaureTextfield;

@end

@implementation ChangeSignaureVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    if ([CLUser shareCLUser].signaure != nil) {
    if([Login curLoginDetailUser].signaure != nil){
//        _signaureTextfield.text = [CLUser shareCLUser].signaure;
        _signaureTextfield.text = [Login curLoginDetailUser].signaure;
    }
    self.navigationItem.title = @"签名";
    [self setAllUIKitAttributes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setAllUIKitAttributes
{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 65, 30);
//    [rightBtn setBackgroundImage:[UIImage imageNamed:@"regnext"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    flexSpacer.width = flexSpacer.width - 20;
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
    
    [_signaureTextfield.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [_signaureTextfield.layer setBorderWidth:1.0];
    [_signaureTextfield.layer setCornerRadius:7];

}

- (void)commitAction:(UIButton *)button
{
    [self.view endEditing:YES];
    
    NSMutableDictionary *CheckParams = [NSMutableDictionary dictionaryWithCapacity:4];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:_signaureTextfield.text forKey:@"value"];
    [CheckParams setObject:@"signaure" forKey:@"field"];
    
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    
    if (_signaureTextfield.text.length > 30) {
        [Config showHUDWithText:@"字数超出限制" andImage:@"error"];
    }else
    {
        [MyDataService requestURL:@"edit_profile.php" httpMethod:@"POST" params:CheckParams fileDatas:nil complection:^(id result)
         {
            NSLog(@"In Signaure %@", result);
            if ([result[@"response"] isEqualToString:@"edit_profile"])
            {
//                [CLUser shareCLUser].signaure = _signaureTextfield.text;
                [Login curLoginDetailUser].signaure = _signaureTextfield.text;
                [self.navigationController popViewControllerAnimated:YES];
            }
            else if([result[@"response"] isEqualToString:@"error"])
            {
                NSDictionary *dic = result[@"error"];
                [Config showHUDWithText:dic[@"text"] andImage:@"error"];
            }
        } failureAction:^{
            [Config showHUDWithText:@"个性签名修改失败" andImage:@"error"];
        }];
    }
    
}

@end
